﻿using PLM.ConfigurationCenter.Model;
using PLM.ConfigurationCenter.SqlSugar;
using Sunny.UI;
using System;
using System.Windows.Forms;

namespace PLM.ConfigurationCenter {
    public partial class SystemSettingForm : UIForm {

        public SystemSettingForm() {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SystemSettingForm_Load(object sender, EventArgs e) {
            var m = DbManager.DbContext.Queryable<SystemSettingModel>().Where(p => p.Name == "RootDic").First();
            if (m != null) {
                this.uiTextBox1.Text = m.Value;
            }
            var autostart = DbManager.DbContext.Queryable<SystemSettingModel>().Where(p => p.Name == "AutoStart").First();
            if (autostart != null) {
                this.uiSwitch1.Active = autostart.Value.ToBoolean();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_selectfolder_Click(object sender, EventArgs e) {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择服务所在文件夹";
            if (dialog.ShowDialog() == DialogResult.OK) {
                if (string.IsNullOrEmpty(dialog.SelectedPath)) {
                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }
                this.uiTextBox1.Text = dialog.SelectedPath;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Save_Click_1(object sender, EventArgs e) {
            var m = DbManager.DbContext.Queryable<SystemSettingModel>().Where(p => p.Name == "RootDic").First();
            if (m == null) {
                m = new SystemSettingModel {
                    Name = "RootDic",
                    Value = this.uiTextBox1.Text.Trim()
                };
                DbManager.DbContext.Insertable(m).ExecuteCommand();
            } else {
                m.Value = this.uiTextBox1.Text.Trim();
                DbManager.DbContext.Updateable(m).ExecuteCommand();
            }

            var autoStart = DbManager.DbContext.Queryable<SystemSettingModel>().Where(p => p.Name == "AutoStart").First();
            if (autoStart == null) {
                autoStart = new SystemSettingModel {
                    Name = "AutoStart",
                    Value = this.uiSwitch1.Active.ToString()
                };
                DbManager.DbContext.Insertable(autoStart).ExecuteCommand();
            } else {
                autoStart.Value = this.uiSwitch1.Active.ToString();
                DbManager.DbContext.Updateable(autoStart).ExecuteCommand();
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
